// src/config/map.ts

import AMapLoader from '@amap/amap-jsapi-loader'

declare global {
  interface Window {
    AMap: any
    _AMapSecurityConfig: {
      securityJsCode: string
    }
  }
}

export const MAP_CONFIG = {
    // 高德地图配置
    KEY: 'aea9cc042a0024a91cc095349d5fa49a',
    VERSION: '2.0',
    // Web端安全密钥
    SECURITY_CODE: '746a86ffcc388980961bdfc5fb9097fb',
    // 地图默认配置
    DEFAULT_OPTIONS: {
      zoom: 11,
      mapStyle: 'amap://styles/normal'
    },
    DEFAULT_CENTER: [34.3645811, 107.2332184] as [number, number],
    DEFAULT_ZOOM: 11,
    PLUGINS: ['AMap.Scale', 'AMap.ToolBar', 'AMap.Driving', 'AMap.Walking', 'AMap.Transfer', 'AMap.MarkerCluster']
}

export const initAMapLoader = async () => {
  if (window.AMap) {
    await AMapLoader.reset()
  }
  
  window._AMapSecurityConfig = {
    securityJsCode: MAP_CONFIG.SECURITY_CODE
  }
  
  return AMapLoader.load({
    key: MAP_CONFIG.KEY,
    version: MAP_CONFIG.VERSION,
    plugins: MAP_CONFIG.PLUGINS,
    AMapUI: {
      version: '1.1',
      plugins: []
    },
    Loca: {
      version: '2.0'
    }
  })
}

export const TRAVEL_MODES = {
  DRIVING: {
    value: 'driving',
    label: '驾车',
    policy: 0 // 最快路线
  },
  WALKING: {
    value: 'walking',
    label: '步行'
  },
  TRANSIT: {
    value: 'transit',
    label: '公交',
    policy: 0 // 最快路线
  }
} as const